<?php
/* 万年祝日カレンダークラス by martin $ 2009/07/20 01:10:24
*/
class Holiday {
 var $holidays;     // yyyymmdd=>name 形式

 function Holiday(){ //インスタンス
  global $D;
  $this->holidays = array();
  $this->add(OD.'anniv.ini.php');
  $this->setHoliday($D);
 }
 function holidayList($y){ // デフォルトの祝祭日のリストを作成（ハッピーマンデー対応）
  $this->holidays = array(
   $y."0101"=>"元日",
   $y.$this->getMonday($y, 1, 2)=>"成人の日",
   $y."0211"=>"建国記念の日",
   $y."03".Intval(20.8431 + 0.242194*($y-1980) - intval(($y-1980)/4))=>"春分の日",
   $y."0429"=>"昭和の日",
   $y."0503"=>"憲法記念日",
   $y."0504"=>"みどりの日",
   $y."0505"=>"こどもの日",
   $y.$this->getMonday($y, 7, 3)=>"海の日",
   $y.$this->getMonday($y, 9, 3)=>"敬老の日",
   $y."09".Intval(23.2488 + 0.242194*($y-1980) - intval(($y-1980)/4))=>"秋分の日",
   $y.$this->getMonday($y, 10, 2)=>"体育の日",
   $y."1103"=>"文化の日",
   $y."1123"=>"勤労感謝の日",
   $y."1223"=>"天皇誕生日"
  );
  return $this->holidays;
 }
 function setHoliday($_D){ // 振替休日や追加祝日も考慮した祝日リストを作成
  $y = $_D['year']; $AutumnHoliday = array();
  $hl = $this->holidayList($y);
  foreach($hl as $key=>$v){
   $y = substr($key,0,4);
   $m = substr($key,4,2);
   $d = substr($key,6,2);
   if(date("w", mktime(0,0,0,$m, $d, $y)) == '0' && !isset($hl[date("Ymd", mktime(0,0,0,$m, $d+1, $y))])){
    $this->holidays[date("Ymd", mktime(0,0,0,$m, $d+1, $y))] = '振替休日';
   }
   if(date("w", mktime(0,0,0,5,3,$y)) == '0' || date("w", mktime(0,0,0,5,4,$y)) == '0' || date("w", mktime(0,0,0,5,5,$y)) == '0'){
    $this->holidays[date("Ymd", mktime(0,0,0,5,6,$y))] = '振替休日';
   }
   if($m == '09' && (date("w", mktime(0,0,0,$m, $d, $y)) == '1' || date("w", mktime(0,0,0,$m, $d, $y)) == '3') ){
    $AutumnHoliday[] = $key;
   }
  }
  if(sizeof($AutumnHoliday) == 2) $this->holidays[intval($AutumnHoliday[0]) + 1] = '国民の休日';
  if(isset($this->myholidays)){
   foreach($this->myholidays as $key=>$v){
    if(array_key_exists($key, $this->holidays)) $v = $v ." ".$this->holidays[$key];
    $this->holidays["$key"] = $v;
   }
  }
  ksort($this->holidays, SORT_NUMERIC);
  unset($this->myholidays);
  return $this->holidays;
 }
 function add($inc){ // 外部ファイルから休日の追加
  $this->myholidays = array();
  if(is_file($inc)){
   include_once(OD.'anniv.ini.php'); # get::$ANNIV_LIST
  } else return;
  foreach($ANNIV_LIST as $v){
   if(preg_match('/[0-9]{8}\|/', $v)){
    list($date, $name) = explode("|", $v);
    $this->myholidays[$date] = trim($name);
   }
  }
  return $this->myholidays;
 }
 function getMonday($y, $m, $wk) { // 第wk週目の月曜日の日付を返す
  $th = array(1=>"first", 2 =>"second", 3 =>"third");
  $utime = strtotime("$th[$wk] Monday", mktime(0,0,0,$m,1,$y));
  return date('md', $utime);
 }
}

$hd = new Holiday(); // これでセット
?>